/*
Copyright 2021 LuJun

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is furnished 
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in 
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/// <reference path="../utils/js.ts" />

namespace lcc {

const TAG = "lcc/base/reference.ts";

/**
 * @zh
 * 框架错误
 */
export namespace ERROR {
	/**
	 * @zh
	 * 引用冲突
	 */
	export let REFERENCE_CONFLICT = true;
};

/**
 * @zh
 * 引用表
 */
const ReferenceMap:{[key:string]:any} = utils.js.createMap();

/**
 * @zh
 * 获得指定引用
 * 
 * @param key 键
 */
export function getRef<T = any>(key:string){
    return <T>ReferenceMap[key];
}

/**
 * @zh
 * 添加指定引用
 * 
 * @param key 键
 * @param value 值
 */
export function addRef(key:string, value:any){
    if(ReferenceMap[key]){
        let msg = `key ${key} conflict`;
        if(ERROR.REFERENCE_CONFLICT){
            __cc__.error(TAG, msg);
        }else{
            __cc__.warn(TAG, msg);
        }
    }
    ReferenceMap[key] = value;
}

/**
 * @zh
 * 移除指定引用
 * 
 * @param key 键
 */
export function removeRef(key:string){
    delete ReferenceMap[key];
}

/**
 * @zh
 * 遍历引用
 * 
 * @param callback - 回调函数
 */
export function foreachRef(callback:(key:string, value:any)=>boolean){
    for(let k in ReferenceMap){
        if(callback(k, ReferenceMap[k])){
            break;
        }
    }
}

/**
 * @zh
 * 清除引用
 * 
 * @param keyReg 正则表达式
 */
export function clearRefs(keyReg:RegExp){
    for(let key of Object.keys(ReferenceMap)){
        if(keyReg.test(key)){
            this.removeRef(key);
        }
    }
}

/**
 * @zh
 * 装饰器
 */
export namespace _decorator {

/**
 * @zh
 * 引用类配置
 */
export interface RefClassConfig {
	/**
	 * @zh
	 * 路径
	 */
	path?:string;

	/**
	 * @zh
	 * 包名
	 */
	pack?:string;
}

/**
 * @zh
 * lcc引用类装饰器
 */
export function refclass(params?:string | RefClassConfig) {
	return function(target:any){
		params = params || target.prototype.constructor.name;
		if(typeof params == "string"){
			addRef(params, target);
		}else {
			let refpath = <RefClassConfig>params;
			refpath.pack = refpath.pack != null ? refpath.pack + "://" : "";
			refpath.path = refpath.path || target.prototype.constructor.name;
			let key = refpath.pack + refpath.path;
			addRef(key, target);
		}
	}
};

}

}
